home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer 2.0 / Internet Surfer 2.0 (Wayzata Technology) (1996).iso / pc / textfile / faqs / c_faq / diff < prev    next >
Encoding:
Internet Message Format  |  1992-12-26  |  14.5 KB

  1. Xref: bloom-picayune.mit.edu comp.lang.c:59948 news.answers:3846
  2. Newsgroups: comp.lang.c,news.answers
  3. Path: bloom-picayune.mit.edu!adam.mit.edu!scs
  4. From: scs@adam.mit.edu (Steve Summit)
  5. Subject: comp.lang.c Changes to Answers to Frequently Asked Questions (FAQ)
  6. Message-ID: <1992Nov3.010510.14974@athena.mit.edu>
  7. Followup-To: poster
  8. Sender: news@athena.mit.edu (News system)
  9. Supersedes: <1992Oct1.210814.22950@athena.mit.edu>
  10. Nntp-Posting-Host: adam.mit.edu
  11. Reply-To: scs@adam.mit.edu
  12. X-Archive-Name: C-faq/diff
  13. Organization: none, at the moment
  14. Date: Tue, 3 Nov 1992 01:05:10 GMT
  15. Approved: news-answers-request@MIT.Edu
  16. Lines: 340
  17.  
  18. Archive-name: C-faq/diff
  19.  
  20. This article contains changes between the previous revision of
  21. the comp.lang.c frequently-asked questions list (posted on
  22. October 1) and the new one.  (Do _not_ worry if you have not seen
  23. the new one yet; it's coming up next.)  As usual, these diffs
  24. have been edited for readability, and are not suitable for use
  25. with the patch program.
  26.  
  27. The changes this month are mostly minor and/or cosmetic.  The
  28. only mildly significant and/or interesting one is that I had
  29. somehow managed to add the same question twice (what had been 2.5
  30. and 2.6); thanks to Mark Brader for pointing this out.
  31.  
  32. ==========
  33. < [Last modified October 1, 1992 by scs.]
  34. ---
  35. > [Last modified November 2, 1992 by scs.]
  36. ==========
  37.     pointer arguments.  To generate a null pointer in a function
  38. <     call context, an explicit cast is typically required:
  39. ---
  40. >     call context, an explicit cast is typically required, to force
  41. >     the 0 to be in a pointer context:
  42. ==========
  43.     6.    The "null string," which is another name for an empty
  44. <         string.  The term "null string" can be confusing in C
  45. ---
  46. >         string ("").  The term "null string" can be confusing in
  47. ==========
  48. <     (The exceptions are when the array is the operand of a sizeof()
  49. <     or & operator, or is a literal string initializer for a
  50. ---
  51. >     (The exceptions are when the array is the operand of a sizeof or
  52. >     & operator, or is a literal string initializer for a character
  53. ==========
  54.   A:    Since arrays decay immediately into pointers, an array is never
  55. <     actually passed to a function.  Therefore, any parameter
  56. ---
  57. >     actually passed to a function.  As a convenience, any parameter
  58. ==========
  59. < 2.5:    Why doesn't sizeof() properly report the size of an array that's
  60. <     a parameter to a subroutine?
  61. <
  62. < A:    sizeof() reports the size of the pointer parameter which the
  63. <     subroutine really receives (see question 2.4).
  64. <
  65. < 2.6:    Why doesn't sizeof(array) work when the array is a function
  66. <     parameter?
  67. < A:    The compiler pretends that the array parameter was declared as a
  68. <     pointer (see question 2.4), and sizeof reports the size of the
  69. <     pointer.
  70. ---
  71. > 2.5:    Why doesn't sizeof properly report the size of an array which is
  72. >     a parameter to a function?
  73. >
  74. > A:    The sizeof operator reports the size of the pointer parameter
  75. >     which the function actually receives (see question 2.4).
  76. ==========
  77.     no longer needed.  You must also be extremely cautious when
  78. <     passing dynamically-allocated arrays down to other subroutines,
  79. <     if those subroutines are also to accept conventional,
  80. ---
  81. >     passing dynamically-allocated arrays down to other functions, if
  82. >     those functions are also to accept conventional, statically-
  83. ==========
  84. < 2.13:    I passed a pointer to a subroutine which initialized it:
  85. ---
  86. > 2.12:    I passed a pointer to a function which initialized it:
  87. ==========
  88.             static int dummy;
  89. <             ip = &i;
  90. ---
  91. >             ip = &dummy;
  92. ==========
  93. < A:    Did the subroutine try to initialize the pointer itself, or just
  94. ---
  95. > A:    Did the function try to initialize the pointer itself, or just
  96. ==========
  97. <     value.  The called subroutine altered only the passed copy of
  98. <     the pointer.  You'll want to pass the address of the pointer
  99. <     (the subroutine will end up accepting a pointer-to-a-pointer).
  100. ---
  101. >     value.  The called function altered only the passed copy of the
  102. >     pointer.  You'll want to pass the address of the pointer (the
  103. >     function will end up accepting a pointer-to-a-pointer).
  104. ==========
  105. < 3.4:    I have a function that returns a string, and it seems to work
  106. <     fine under the debugger, but when it returns to its caller, the
  107. <     returned string is garbage.
  108. ---
  109. > 3.4:    I have a function that is supposed to return a string, but when
  110. >     it returns to its caller, the returned string is garbage.
  111. ==========
  112.   A:    Under C's integral promotion rules, the multiplication is
  113. <     carried out using integer arithmetic, and the result overflows
  114. <     and/or is truncated before being assigned to the long int left-
  115. ---
  116. >     carried out using int arithmetic, and the result may overflow
  117. >     and/or be truncated before being assigned to the long int left-
  118. ==========
  119.     "int func(x) float x;".  Old C (and ANSI C, in the absence of
  120. <     prototypes) silently promotes floats to doubles when passing
  121. <     them as arguments, and arranges that doubles being passed are
  122. <     coerced back to floats if the formal parameters are declared
  123. <     that way.
  124. ---
  125. >     prototypes, and in variable-length argument lists) "widens"
  126. >     certain arguments when they are passed to functions.  floats
  127. >     are promoted to double, and characters and short integers are
  128. >     promoted to integers.  (The values are automatically coerced
  129. >     back to the corresponding narrower types within the body of the
  130. >     called function, if they are declared that way there.)
  131. ==========
  132. <     rely on such an extension?  It is usually a bad idea to try to
  133. <     determine language properties, especially pertaining to the ANSI
  134. <     Standard, by performing experiments with a particular compiler.
  135. ---
  136. >     rely on such an extension?  It is usually a bad idea to perform
  137. >     experiments with a particular compiler to determine properties
  138. >     of a language; the applicable standard may permit variations, or
  139. >     the compiler may be wrong.
  140. ==========
  141.   A:    There is no good answer to this question.  If the values are
  142.     integers, a well-known trick using exclusive-OR could perhaps be
  143.     used, but it will not work for floating-point values or
  144. <     pointers, (and it will not work if the two values are the same
  145. <     variable, and the "obvious" supercompressed implementation for
  146. <     integral types a^=b^=a^=b is, strictly speaking, illegal due to
  147. <     multiple side-effects, and...).  If the macro is intended to be
  148. ---
  149. >     pointers, or if the two values are the same variable (and the
  150. >     "obvious" supercompressed implementation for integral types
  151. >     a^=b^=a^=b is in fact illegal due to multiple side-effects,
  152. >     and...).  If the macro is intended to be used on values of
  153. ==========
  154.     The best all-around solution is probably to forget about using a
  155. <     macro, unless you don't mind passing in the type as a third
  156. ---
  157. >     macro, unless you're willing to pass in the type as a third
  158. ==========
  159. < 6.5:    Does sizeof() work in preprocessor #if directives?
  160. ---
  161. > 6.5:    Does the sizeof operator work in preprocessor #if directives?
  162. ==========
  163.     compilation, before type names have been parsed.  Consider using
  164. <     the predefined constants in <limits.h>, or a "configure" script,
  165. ---
  166. >     the predefined constants in ANSI's <limits.h>, or a "configure"
  167. ==========
  168. <         #include <stddef.h>        /* for NULL, size_t */
  169. <         #include <stdlib.h>        /* for malloc */
  170. ---
  171. >         #include <stdlib.h>        /* for malloc, NULL, size_t */
  172. ==========
  173.     Under a pre-ANSI compiler, rewrite the function definition
  174.     without a prototype ("char *vstrcat(first) char *first; {"),
  175. <     include <stdio.h> rather than <stddef.h>, replace "#include
  176. <     <stdlib.h>" with "extern char *malloc();", and use int instead
  177. ---
  178. >     include <stdio.h> rather than <stdlib.h>, add "extern
  179. >     char *malloc();", and use int instead of size_t.  You may also
  180. ==========
  181. >     Remember that in variable-length argument lists, function
  182. >     prototypes do not supply parameter type information; therefore,
  183. >     default argument promotions apply (see question 5.6), and null
  184. >     pointer arguments must be typed explicitly (see question 1.2).
  185. ==========
  186. < A:    Structures may have this padding, so that alignment properties
  187. <     will be preserved when an array of contiguous structures is
  188. <     allocated.
  189. ---
  190. > A:    Structures may have this padding (as well as internal padding;
  191. >     see also question 9.5), so that alignment properties will be
  192. >     preserved when an array of contiguous structures is allocated.
  193. ==========
  194. < 9.10:    How can I turn off structure padding, so that I can get a struct
  195. <     to conform to an externally-imposed storage layout?
  196. ---
  197. > 9.10:    My compiler is leaving holes in structures, which is wasting
  198. >     space and preventing "binary" I/O to external data files.  Can I
  199. >     turn off the padding, or otherwise control the alignment of
  200. >     structs?
  201. ==========
  202. <     attempting to conform to some externally-imposted storage
  203. ---
  204. >     attempting to conform to some externally-imposed storage layout,
  205. ==========
  206. <     It is usually better to use fgets() to read a whole line, and
  207. <     then use sscanf() or other string functions to pick apart the
  208. <     line buffer.
  209. ---
  210. >     It is usually better to use fgets to read a whole line, and
  211. >     then use sscanf or other string functions to pick apart the line
  212. >     buffer.  If you do use sscanf, don't forget to check the return
  213. >     value to make sure that the expected number of items were found.
  214. ==========
  215. <     links).  It is best to remember the names of open files yourself
  216. <     (perhaps with a wrapper function around fopen).
  217. ---
  218. >     links).  It is best to remember the names of files yourself when
  219. >     you open them (perhaps with a wrapper function around fopen).
  220. ==========
  221. < 12.7:    Each time I run my program, I get the same sequence of random
  222. <     numbers.
  223. ---
  224. > 12.7:    Each time I run my program, I get the same sequence of numbers
  225. >     back from rand().
  226. ==========
  227.   A:    You can call srand() to seed the pseudo-random number generator
  228. <     with a more random value.  Popular random initial seeds are the
  229. ---
  230. >     with a more random initial value.  Popular random initial seeds
  231. ==========
  232. < A:    Try running lint first.  Many C compilers are really only half-
  233. ---
  234. > A:    Try running lint first (perhaps with the -a, -c, -h, -p and/or
  235. >     other options).  Many C compilers are really only half-
  236. ==========
  237.   A:    Make sure you're linking against the correct math library.  For
  238. <     instance, under Unix, you usually need to add -lm after the
  239. <     source and object files when compiling/linking.
  240. ---
  241. >     instance, under Unix, you usually need to use the -lm option at
  242. >     the end of the command line when compiling/linking.
  243. ==========
  244.   A:    You can #include <math.h> and use the pow() function, although
  245. <     explicit multiplication is often better for small integral
  246. <     exponents.
  247. ---
  248. >     explicit multiplication is often better for small positive
  249. >     integral exponents.
  250. ==========
  251.   A:    BSD systems provide ftruncate(), several others supply chsize(),
  252.     and a few may provide a (possibly undocumented) fcntl option
  253.     F_FREESP.  Under MS-DOS, you can sometimes use
  254. <     write(fd, (char *)NULL, 0).  However, but there is no truly
  255. <     portable solution.
  256. ---
  257. >     write(fd, "x", 0).  However, there is no truly portable
  258. >     solution.
  259. ==========
  260. < 17.3:    How can I return several values from a subroutine?
  261. ---
  262. > 17.3:    How can I return several values from a function?
  263. ==========
  264. < A:    Either pass pointers which the subroutine can fill in, or have
  265. <     the subroutine return a structure containing the desired values.
  266. ---
  267. > A:    Either pass pointers to locations which the function can fill
  268. >     in, or have the function return a structure containing the
  269. ==========
  270. < 17.7:    Does anyone know of a program for converting Pascal (FORTRAN,
  271. <     LISP, "Old" C, ...) to C?
  272. ---
  273. > 17.7:    Does anyone know of a program for converting Pascal or FORTRAN
  274. >     (or LISP, Ada, AWK, "Old" C, ...) to C?
  275. ==========
  276. <     A PL/M to C converter was posted to alt.sources in April, 1991.
  277. <
  278. <     The following companies sell various translation tools and
  279. <     services:
  280. <         Cobalt Blue
  281. <         2940 Union Ave., Suite C
  282. <         San Jose, CA  95124  USA
  283. <         (+1) 408 723 0474
  284. <         Promula Development Corp.
  285. <         3620 N. High St., Suite 301
  286. <         Columbus, OH  43214  USA
  287. <         (+1) 614 263 5454
  288. <         Micro-Processor Services Inc.
  289. <         92 Stone Hurst Lane
  290. <         Dix Hills, NY  11746  USA
  291. <         (+1) 519 499 4461
  292. ---
  293. >     This FAQ list's maintainer also has available a list of other
  294. >     commercial translation products, and some for more obscure
  295. >     languages.
  296. ==========
  297.   A:    The contest typically runs from early March through mid-May.  To
  298.     obtain a current copy of the rules and other information, send
  299.     e-mail with the Subject: line "send rules" to:
  300.  
  301. <         {apple,pyramid,sun,uunet}!hoptoad!judges  or  judges@toad.com
  302. ---
  303. >         {apple,pyramid,sun,uunet}!hoptoad!obfuscate  or
  304. >         obfuscate@toad.com
  305. ==========
  306.   A:    Use arrays of char or int, with a few macros to access the right
  307. <     bit at the right index:
  308. ---
  309. >     bit at the right index (try using 8 for CHAR_BIT if you don't
  310. >     have <limits.h>):
  311. ==========
  312. <         #define Mask(bit) (1 << ((bit) % CHAR_BIT))
  313. <         #define Slot(bit) ((bit) / CHAR_BIT)
  314. <         #define Set(ary, bit) ((ary)[Slot(bit)] |= Mask(bit))
  315. <         #define Test(ary, bit) ((ary)[Slot(bit)] & Mask(bit))
  316. ---
  317. >         #define BITMASK(bit) (1 << ((bit) % CHAR_BIT))
  318. >         #define BITSLOT(bit) ((bit) / CHAR_BIT)
  319. >         #define BITSET(ary, bit) ((ary)[BITSLOT(bit)] |= BITMASK(bit))
  320. >         #define BITTEST(ary, bit) ((ary)[BITSLOT(bit)] & BITMASK(bit))
  321. ==========
  322. > 17.19: How do you pronounce "char"?
  323. >
  324. > A:    You can pronounce the C keyword "char" in at least three ways:
  325. >     like the English words "char," "care," or "car;" the choice is
  326. >     arbitrary.
  327. ==========
  328. > Knuth    Donald E. Knuth, The Art of Computer Programming, (3 vols.),
  329. >     Addison Wesley, 1981.
  330. ==========
  331.   Thanks to Jamshid Afshar, Sudheer Apte, Dan Bernstein, Stan Brown, Joe
  332.   Buehler, Burkhard Burow, D'Arcy J.M. Cain, Raymond Chen, Christopher
  333.   Calabrese, James Davies, Norm Diamond, Ray Dunn, Stephen M. Dunn, Bjorn
  334.   Engsig, Dave Gillespie, Samuel Goldstein, Alasdair Grant, Ron Guilmette,
  335.   Doug Gwyn, Tony Hansen, Joe Harrington, Guy Harris, Jos Horsmeier, Blair
  336.   Houghton, Kirk Johnson, Peter Klausler, Andrew Koenig, Tom Koenig, John
  337. > Lauro, Don Libes, Christopher Lott, Tim McDaniel, Evan Manning, Barry
  338. > Margolin, Mark Moraes, Darren Morby, Richard A. O'Keefe, Hans Olsson,
  339.   Francois Pinard, randall@virginia, Pat Rankin, Rich Salz, Chip
  340.   Salzenberg, Paul Sand, Doug Schmidt, Patricia Shanahan, Peter da Silva,
  341.   Joshua Simons, Henry Spencer, Erik Talvola, Clarke Thatcher, Chris
  342.   Torek, Goran Uddeborg, Wietse Venema, Ed Vielmetti, Larry Virden, Freek
  343.   Wiedijk, and Dave Wolverton, who have contributed, directly or
  344.   indirectly, to this article.  Special thanks to Karl Heuer, and
  345.   particularly to Mark Brader, who (to borrow a line from Steve Johnson)
  346.   have goaded me beyond my inclination, and occasionally beyond my
  347.   endurance, in relentless pursuit of a better FAQ list.
  348. ==========
  349.  
  350.                     Steve Summit
  351.                     scs@adam.mit.edu
  352.                     scs%adam.mit.edu@mit.edu
  353.                     mit-eddie!adam.mit.edu!scs
  354.